package com.batdream.message.netty;

import io.netty.channel.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Wang Chen Peng
 * @date 2018/12/30
 * describe: 关联用户与通道的容器
 */
@Component
@Log4j2
public class UserChannelContext {

    private static ConcurrentHashMap<String, Channel> context;

    static {
        context = new ConcurrentHashMap<>();
    }

    public Channel getChannel(String userId) {
        return context.get(userId);
    }

    public void addChannel(String userId, Channel channel) {
        log.debug("UserChannelContext容器添加 userId : {}", userId);
        context.put(userId, channel);
    }

    public boolean containsKey(String userId) {
        return context.containsKey(userId);
    }

    public void outPut() {
        context.forEach((key, value) -> {
            System.out.println(" ConcurrentHashMap:key : " + key + "   ");
            System.out.println(" ConcurrentHashMap:value : " + value.id().asLongText());
        });
    }

    public void  remove(String userId){
        if (containsKey(userId)){
            log.debug("UserChannelContext容器移除 userId : {}", userId);
            context.remove(userId);
        }
    }


}
